#include <allegro.h>
#include "general.h"
#include "collision.h"


int jeu()
{
 
// is true if the player is on the ground

BITMAP *Buffer;
BITMAP *Background1;//walking background declaration
Personnage p;
coins c,c1,c2,c3,c4,c5; //coins declaration
obstacle o1;
int i,j,compteur;
int b;

allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
Buffer = create_bitmap(800, 600);



//chargement du background
Background1 = load_bitmap("./images/background1.bmp", NULL);

//chargement des images
chargement_image_personnage(&p);

//chargement coins
chargement_coins(&c1);
chargement_coins(&c2);
chargement_coins(&c3);
chargement_coins(&c4);
chargement_coins(&c5);


//chargement obstacles
chargement_obstacle(&o1);




//initialisation des variables

//obstacles

o1.x = 800;
o1.y = 520;
o1.n=1;


//Coins
c1.x = 100;			
c1.y = 370;
c1.n=1;

c2.x = 300;
c2.y = 370;
c2.n=1;

c3.x = 400;
c3.y = 370;
c3.n=1;

c4.x = 600;
c4.y = 480;
c4.n=1;

c5.x = 700;
c5.y = 480;
c5.n=1;



p.x = 30;
p.y = 440;
p.direction=0;
i=0;


while (!key[KEY_ESC]) {

//personnage
int sprite1_x=p.x;
int sprite1_y=p.y;
int sprite1_w=p.img_right[0]->w;
int sprite1_h=p.img_right[0]->h;



//obstacle1
int spriteo1_x=o1.x;
int spriteo1_y=o1.y;
int spriteo1_w=o1.img_obstacle->w;   
int spriteo1_h=o1.img_obstacle->h;


//coins1
int sprite2_x=c1.x;
int sprite2_y=c1.y;
int sprite2_w=c1.img_coins->w;   
int sprite2_h=c1.img_coins->h;
 
//coins2
int sprite3_x=c2.x;
int sprite3_y=c2.y;
int sprite3_w=c2.img_coins->w;   
int sprite3_h=c2.img_coins->h;

//coins3
int sprite4_x=c3.x;
int sprite4_y=c3.y;
int sprite4_w=c3.img_coins->w;   
int sprite4_h=c3.img_coins->h;

//coins4
int sprite5_x=c4.x;
int sprite5_y=c4.y;
int sprite5_w=c4.img_coins->w;   
int sprite5_h=c4.img_coins->h;

//coins5
int sprite6_x=c5.x;
int sprite6_y=c5.y;
int sprite6_w=c5.img_coins->w;   
int sprite6_h=c5.img_coins->h;



//dessiner le background

blit(Background1, Buffer, b, 0, 0, 0, Background1->w, Background1->h);


//tester s'il ya collision
//Coins :
//srand( (unsigned)time( NULL ) );
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite2_x,  sprite2_y,  sprite2_w,  sprite2_h) !=0 )
	{ 
 c1.n=1;c1.x+=800;}  
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite3_x,  sprite3_y,  sprite3_w,  sprite3_h) !=0 )
	{
 c2.n=1;c2.x+=800;}  
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite4_x,  sprite4_y,  sprite4_w,  sprite4_h) !=0 ) 
	{
  c3.n=1;c3.x+=800;} 
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite5_x,  sprite5_y,  sprite5_w,  sprite5_h) !=0 )
	{
  c4.n=1;c4.x+=800;} 
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  sprite6_x,  sprite6_y,  sprite6_w,  sprite6_h) !=0 )
	{
  c5.n=1;c5.x+=800;} 



//Obstacle :
if(bounding_box_collision(sprite1_x, sprite1_y, sprite1_w, sprite1_h,  spriteo1_x,  spriteo1_y,  spriteo1_w,  spriteo1_h) !=0 )    o1.n=2;  




//dessiner les coins s'il n'ya pas collision
if (p.x<1000)
{

if(c1.n==1){

draw_sprite(Buffer, c1.img_coins, c1.x ,c1.y);
}

if(c2.n==1){

draw_sprite(Buffer, c2.img_coins, c2.x ,c2.y);
}

if(c3.n==1){

draw_sprite(Buffer, c3.img_coins, c3.x ,c3.y);
}

if(c4.n==1){

draw_sprite(Buffer, c4.img_coins, c4.x ,c4.y);
}

if(c5.n==1){

draw_sprite(Buffer, c5.img_coins, c5.x ,c5.y);
}



}
else
{c1.n=2;c2.n=2;c3.n=2;c4.n=2;c5.n=2;}






///////////////////////////////////////

//dessiner l'obstacle s'il n'ya pas collision
if(o1.n==1){
draw_sprite(Buffer, o1.img_obstacle, o1.x ,o1.y);
}



//Mouvement

o1.x--;
o1.x--;

if (o1.n==2)
{o1.x+=8;}


//Keybord Control

if (key[KEY_RIGHT] && p.x < SCREEN_W - p.img_up->w ) {

i++;
o1.x--;
o1.x--;
o1.x--;



if(b<2400){b+=2;}
if (b>=2200){i++;p.x++;}


//coins
//coins speed
c1.x--;
c1.x--;
c2.x--;
c2.x--;
c3.x--;
c3.x--;
c4.x--;
c4.x--;
c5.x--;
c5.x--;

if(b>=2400){
c1.x+=2;
c2.x+=2;
c3.x+=2;
c4.x+=2;
c5.x+=2;
		}
//Obstacle



p.direction=0;
}






if (key[KEY_SPACE])
	
	{do{p.y--;}while(p.y>=350);if(p.y==350){key[KEY_SPACE]=0;}}


	if(p.y<440){p.y++;} 


	


	







//dessiner le personnage suivant la direction

if(p.direction==0)	
			{

draw_sprite(Buffer, p.img_right[i%3], p.x, p.y);
			}
else if(p.direction==2)
{


draw_sprite(Buffer, p.img_right[i%3], p.x, p.y);

}

else
{



draw_sprite(Buffer, p.img_right[i%3], p.x, p.y);


}

////////////////////////////////////////

blit(Buffer, screen, 0 , 0, 0, 0, SCREEN_W, SCREEN_H);

clear_bitmap(Buffer);
		
if(i==1000){
i=0;
}

// Pause
// P=Pause
// Ok=Retour

if (key[KEY_P]){


allegro_message("Pause! Press OK to Continue\n");}

if(o1.n==2){allegro_message("Game Over !");return 0;}


if(p.x==530){allegro_message("Level achieved !");
return 1;}

}//fin while



//freeing memory



destroy_bitmap(p.img_right[0]);

destroy_bitmap(p.img_right[1]);


destroy_bitmap(Buffer);
destroy_bitmap(c1.img_coins);
destroy_bitmap(c2.img_coins);
destroy_bitmap(c3.img_coins);
destroy_bitmap(c4.img_coins);
destroy_bitmap(c5.img_coins);


destroy_bitmap(o1.img_obstacle);


return 0;

}//fin main






